package com.icart.controller.personController;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.icart.context.HttpUserContext;
import com.icart.context.provider.UserContextProvider;
import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;
import com.icart.controller.WxUtilController;
import com.icart.entities.CuCustinfoEntity;
import com.icart.repositories.CustinfoRepository;
import com.icart.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;



@Controller
@RequestMapping("/person")
public class PersonLoginController {

    @Autowired
    private CustinfoRepository custinfoRepository;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private RestTemplate template;

    /*登录页面*/
    @RequestMapping("/login")
    public String login(Model model) {
        if(Tools.notEmpty(request.getParameter("flag"))){
            String flag=request.getParameter("flag");
            model.addAttribute("flag",flag);
            model.addAttribute("busiid",request.getParameter("busiid"));
            model.addAttribute("shopid",request.getParameter("shopid"));
        }
        return "person/login/login";
    }

    /*点击登录查手机号码是否存在用户*/
    @RequestMapping("/logincheck")
    @ResponseBody
    public String logincheck(@RequestParam(name = "mobile",required = false) String mobile,Model model) {
        if(!mobile.equals("")){
            String openid=request.getSession().getAttribute("openid").toString();
            WxUtilController wx=new WxUtilController();
            Object user=wx.getUser(template,request,openid);
            /*通过电话号码查找客户*/
            CuCustinfoEntity cuCustinfo=custinfoRepository.login(mobile);
            if(cuCustinfo!=null){
                /*设置在session里面*/
                HttpUserContext context=UserContextProvider.create(cuCustinfo.getCustid(),cuCustinfo.getCustname());
                ContextStorage storage=new SessionContextStorageImpl(request);
                storage.save("loginuser",context);
                return "success";
            }else {
                /*获取当前微信的openid,将其与手机号绑定*/
                Integer flag=custinfoRepository.register(mobile,openid);
                CuCustinfoEntity cuCustinfo2=custinfoRepository.login(mobile);
                HttpUserContext context=UserContextProvider.create(cuCustinfo2.getCustid(),cuCustinfo2.getCustname());
                ContextStorage storage=new SessionContextStorageImpl(request);
                storage.save("loginuser",context);
                if(flag>0){
                    return "success";
                }
            }
        }
        return "";
    }

    /*退出登录*/
    @RequestMapping("/exitlogin")
    public String  exitlogin(Model model){
        ContextStorage storage=new SessionContextStorageImpl(request);
        storage.clear("loginuser");
        return "person/login/login";
    }
}